package com.zhudi.develop_demo_as;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.zhudi.dao.DaoMaster;
import com.zhudi.dao.DaoSession;
import com.zhudi.develop.util.ZhudiFileUtil;
import com.zhudi.develop.util.ZhudiToastSingle;
import com.zhudi.develop_demo_as.utils.Variables;

public class MainApplication extends Application {
	private static MainApplication context;
	private static DaoMaster daoMaster;
	private static DaoSession daoSession;

	@Override
	public void onCreate() {
		super.onCreate();
		context = this;
		//遮罩层
		//sLoadingLayer.init(this);
		setFile();
	}

	private void setFile() {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			ZhudiToastSingle.showToast(context,getString(R.string.sd_check_fail), Toast.LENGTH_SHORT,-1);
		} else {
			ZhudiFileUtil.createDir(Variables.APP_PIC_SDPATH);
		}
	}

	public static MainApplication getInstance() {
		return context;
	}

	public static DaoMaster getDaoMaster(Context context) {
		if (daoMaster == null) {
			DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "example.db", null);
			daoMaster = new DaoMaster(helper.getWritableDatabase());
		}
		return daoMaster;
	}

	public static DaoSession getDaoSession(Context context) {
		if (daoSession == null) {
			if (daoMaster == null) {
				daoMaster = getDaoMaster(context);
			}
			daoSession = daoMaster.newSession();
		}
		return daoSession;
	}

}
